package com.example.youjingjing.electronicbalance;

import android.os.AsyncTask;
import android.util.Log;
import android.util.SparseArray;

import com.example.youjingjing.electronicbalance.javaandroid.DBXException;
import com.example.youjingjing.electronicbalance.javaandroid.DSRESTConnection;
import com.example.youjingjing.electronicbalance.javaandroid.TDataSet;
import com.example.youjingjing.electronicbalance.javaandroid.TJSONObject;
import com.example.youjingjing.electronicbalance.service.DownloadListener;
import com.example.youjingjing.electronicbalance.utils.JsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * Created by youjingjing on 2017/8/2.
 */

public class MyAscyncTaskDemo extends AsyncTask<String,Integer,Boolean> {
    SparseArray<String> array;
    private MyAscyncTaskDemo myAscyntask;
    private DownloadListener listener;


    public MyAscyncTaskDemo() {
    }

    public MyAscyncTaskDemo(DownloadListener listener) {
        this.listener = listener;
    }

    //    ProgressDialog  progressDialog = new ProgressDialog(context);
    @Override
    protected Boolean doInBackground(String... strings) {
        getServiceInfo(strings[0],strings[1],strings[2],strings[3]);
        publishProgress();
        return true;
    }

    @Override
    protected void onPreExecute() {
        showProgress(true);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
//        progressDialog.setMessage("当前下载进度：" + values[0] + "%");

    }

    @Override
    protected void onPostExecute(Boolean success) {
        super.onPostExecute(success);
        myAscyntask = null;
        showProgress(false);
        if (success) {
//            finish();
//            Toast.makeText(this, "下载成功", Toast.LENGTH_SHORT).show();
        } else {
//            mPasswordView.setError(getString(R.string.error_incorrect_password));
//            mPasswordView.requestFocus();
//            Toast.makeText(this, "下载失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        myAscyntask = null;
        showProgress(false);
    }

    private void showProgress(boolean b) {
        if (b=true){
//            progressDialog.show();
        }else{
//            progressDialog.dismiss();
        }
    }

    private DSRESTConnection getConnection(String host, int port, String username, String password) {

        DSRESTConnection conn = new DSRESTConnection();

        conn.setHost(host);

        conn.setPort(port);

        conn.setUserName(username);

        conn.setPassword(password);

        return conn;
    }
    public void getServiceInfo(String host,String port,String name,String password) {

//                DSRESTConnection conn = getConnection("10.0.11.192",8080,name,password);
        DSRESTConnection conn = getConnection(host, Integer.parseInt(port), name, password);

        GeneratedProxy.TPRT_DS_Interface ds = new GeneratedProxy.TPRT_DS_Interface(conn);


        TDataSet tDataSet = null;
        try {
            tDataSet = ds.GetUserAuthority(name);

            if (tDataSet.next()) {
                TJSONObject tjsonObject = tDataSet.asJSONObject();

//                        Log.e("run: ", "" +  tjsonObject.toString());

                Object userName = tjsonObject.get("UserName").value.getInternalObject();
                String authority = tjsonObject.get("UserAuthority").value.toString();

                authority.replaceAll("\\]|\\[", "");

                JSONTokener jsonParser = new JSONTokener(JsonUtils.serialize(tjsonObject));

                JSONObject person = (JSONObject) jsonParser.nextValue();

                JSONArray elements = person.getJSONArray("Elements");

                array = new SparseArray<>();
                for (int i = 0; i < tDataSet.getColumns().size(); i++) {
                    JSONObject value = elements.getJSONObject(i);
                    JSONObject value1 = value.getJSONObject("value");
                    JSONArray elements1 = value1.getJSONArray("Elements");
                    JSONObject jsonObject = elements1.getJSONObject(0);
                    String n = jsonObject.get("value").toString();
                    Log.e("run: ", "门店号：" + n);
                    array.put(i, n);
                }


//
            } else {
                Log.d("1", "没有数据 ");
            }
        } catch (DBXException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

}
